package neptune.web.server.services.experiment;

import it.robertobifulco.ias.Operation;
import javax.servlet.http.HttpSession;
import neptune.manager.model.topology.Topology;
import neptune.manager.storage.XMLTopologyStorage;
import neptune.web.client.model.topology.SerializableTopology;
import neptune.web.client.services.SecurityException;
import neptune.web.client.services.experiment.TemplateService;
import neptune.web.server.Debug;
import neptune.web.server.operations.DeleteTemplate;
import neptune.web.server.operations.GetAllTemplate;
import neptune.web.server.operations.GetXMLTemplate;
import neptune.web.server.operations.SaveTemplate;
import neptune.web.server.security.SecurityHelper;
import neptune.web.server.services.SerializationHelper;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * 
 * @author Luigi Lorenzo Arpino [luigiarpino@yahoo.it]
 *
 */
public class TemplateServiceImpl extends RemoteServiceServlet implements TemplateService
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see neptune.web.client.services.experiment.TemplateService#getAllTemplate()
	 */
	public String[][] getAllTemplate() throws SecurityException, Exception 
	{
		
		try 
		{
			HttpSession session = getThreadLocalRequest().getSession(false);
			// verifico autorizzazione
			Operation operation = SecurityHelper.getOperation(	GetAllTemplate.ID, session );
			return (String[][]) operation.execute(null)[0];
		}
		catch (SecurityException e) 
		{
			throw new SecurityException(e.getMessage());
		}
		catch (Exception e) 
		{
			Debug.printTrace(e);
			throw new Exception(e);
		}
	}

	/* (non-Javadoc)
	 * @see neptune.web.client.services.experiment.TemplateService#getXMLTemplate(java.lang.String)
	 */
	public String getXMLTemplate(String fileName) throws SecurityException 
	{
		try 
		{
			HttpSession session = getThreadLocalRequest().getSession(false);
			// verifico autorizzazione
			Operation operation = SecurityHelper.getOperation(	GetXMLTemplate.ID, session );
			return (String) operation.execute(new Object[] {fileName })[0];
		}
		catch (Exception e) 
		{
			throw new SecurityException(e.getMessage());
		}

	}

	/* (non-Javadoc)
	 * @see neptune.web.client.services.experiment.TemplateService#getTopology(java.lang.String)
	 */
	public SerializableTopology getTopology(String fileName) throws SecurityException 
	{
		try 
		{
			HttpSession session = getThreadLocalRequest().getSession(false);
			// verifico autorizzazione
			Operation operation = SecurityHelper.getOperation(	GetXMLTemplate.ID, session );
			String xmlTopology =(String) operation.execute(new Object[] {fileName })[0];
			Topology topology = XMLTopologyStorage.getTopologyFromXML(xmlTopology);
			SerializableTopology serializableTopology=SerializationHelper.toSerializableTopology(topology);
			return serializableTopology; 
		}
		catch (Exception e) 
		{	
			throw new SecurityException(e.getMessage());
		}
	}

	/* (non-Javadoc)
	 * @see neptune.web.client.services.experiment.TemplateService#saveTemplate(neptune.web.client.model.topology.SerializableTopology)
	 */
	public Boolean saveTemplate(SerializableTopology serTopology)
			throws SecurityException 
	{
		try 
		{
			HttpSession session = getThreadLocalRequest().getSession(false);
			// verifico autorizzazione
			Operation operation = SecurityHelper.getOperation(	SaveTemplate.ID, session );
			Topology topology = SerializationHelper.toTopology(serTopology);
			Boolean esito =(Boolean) operation.execute(new Object[] {topology})[0];
			return esito; 
		}
		catch (Exception e) 
		{	
			throw new SecurityException(e.getMessage());
		}
	}

	/* (non-Javadoc)
	 * @see neptune.web.client.services.experiment.TemplateService#deleteTemplate(java.lang.String)
	 */
	public Boolean deleteTemplate(String fileName) throws SecurityException 
	{
		try 
		{
			HttpSession session = getThreadLocalRequest().getSession(false);
			// verifico autorizzazione
			Operation operation = SecurityHelper.getOperation(	DeleteTemplate.ID, session );
			operation.execute(new Object[] {fileName});
			return new Boolean(true); 
		}
		catch (Exception e) 
		{	
			throw new SecurityException(e.getMessage());
		}
	}

}
